/**
 * 模态框视图
 */

define(['jquery', 'underscore', 'ModalView',
        'text!templates/common/confirm.html',
        'text!templates/common/confirm_agree.html',
        'text!templates/common/confirm_edit.html',],
    function ($, _, ModalView, confirm, confirmAgree, confirmEdit) {
        var ConfirmView = ModalView.extend({
            el: 'body',
            events: {
                'click                [data-toggle="confirm"]': 'getConfirm',
                'change                              #AgreeDo': 'toggleAgree'
            },
            initialize: function () {
                this.confirmTpl = _.template(confirm);
                this.confirmAgreeTpl = _.template(confirmAgree);
                this.confirmEditTpl = _.template(confirmEdit);
            },
            /**
             * 获取Remote Modal
             * @param event
             */
            getConfirm: function (event) {
                event.preventDefault();
                var $target = $(event.target),
                    $confirmToggle = $target.closest('[data-toggle="confirm"]'),
                    $page = $target.closest('#Page'),
                    confirmParams = {},
                    that = this;

                if ($confirmToggle.length == 0) {
                    $confirmToggle = $target.closest('[data-toggle="operate"]');
                }

                confirmParams.actionUrl = $confirmToggle.data('action') || "#";
                confirmParams.actionMethod = $confirmToggle.data('method') || "#";
                confirmParams.confirmTarget = $confirmToggle.data('confirm-target') || "";
                confirmParams.titleText = $confirmToggle.data('text-title') || "确认";
                confirmParams.messageText = $confirmToggle.data('text-message') || "确认此操作?";
                confirmParams.doText = $confirmToggle.data('text-do') || "确认";
                confirmParams.undoText = $confirmToggle.data('text-undo') || "取消";
                confirmParams.successMode = $confirmToggle.data('success-mode') || "alert";
                confirmParams.errorMode = $confirmToggle.data('error-mode') || "alert";
                confirmParams.refreshTarget = $confirmToggle.data('refresh-target');


                if ($('#Confirm').length > 0) {
                    $('.modal-backdrop').remove();
                    $('#Confirm').remove();
                }

                switch ($confirmToggle.data('confirm-type')) {
                    case 'agree':
                        $page.append(that.confirmAgreeTpl(confirmParams));
                        break;
                    case 'edit':
                        $page.append(that.confirmEditTpl(confirmParams));
                        $('#Do').on('click',function(event){
                            var $target = $(event.target);
                            that.toEdit();
                            $target.closest('.modal').modal('hide');

                        });
                        break;
                    default:
                        $page.append(that.confirmTpl(confirmParams));
                        break;
                }

                $('#Confirm').modal('show');

            },
            toggleAgree: function (event) {
                var $target = $(event.target),
                    $Confirm = $('#Confirm');

                if ($target.is(':checked')) {
                    $Confirm.find('#Do').prop('disabled', false);
                } else {
                    $Confirm.find('#Do').prop('disabled', true);
                }
            },
            toEdit: function (event) {
                this.$('.edit-block .edit-static').addClass('hide');
                this.$('.edit-block .edit-control').removeClass('hide');
            }
        });

        return ConfirmView;
    });
